if (!jsx3.lang.Class.forName("ux.cdb.admin.queries.organ.Query")) {
    
jsx3.Class.defineClass(
    "ux.cdb.admin.queries.organ.Query", jsx3.gui.Block, [],
    function (query) {

		/**
		 * Helper Functions, return a copy function with 
		 * @param fn {Function}
		 * @param scope {Object} 
		 */
		query.prototype.bind = function(fn, scope) {
			scope = scope || this;
			return function() {
				fn.apply(scope, arguments);
			};
		};
		
		/**
		 * Add event to jsx Object
		 * @param name {String} jsx object name or comma-splitted names
		 * @param event {String} event name
		 * @param fct {Function} event handler
		 */
		query.prototype.on = function (name, event, fct) {
			var names = name.split(","), block=this;
			for(var i=0; i<names.length; i++) {
				block = block.get(names[i]);
			}
			block[event] = this.bind(fct);
		};

		query.prototype.initialize = function(){
/* @JSC :: begin DEBUG */
			jsx3.log('organ#Query#initialize');
/* @JSC :: end */
			var me = this;
			this.on("btnQuery", "onClick", me.onQuery);
			this.on("btnReset", "onClick", me.onReset);
			this.queryDetail = this.get('queryDetail');
			this.matrix = this.get('mtxMain');
			this.searchType = this.get('blkSearchType').getChild(1);
			this.searchType.onClick = this.bind(me.selectType, me);
			ux.cdb.admin.CommonCache.loadCache(['INTERNAL_ORG'],me,
				function(){
					me.queryDetail.getChild(0).getChild(1).setSourceXML(ux.cdb.admin.CommonCache.getInstance().getCache('CommonCache_INTERNAL_ORG'));
					me.queryDetail.getChild(0).getChild(1).repaint();
				});

			this.pageSize = 20;
			this.totalRecordCount = 0;
			this.currentPage = 1;
			this.totalPages = 1; 
			this.pageLoaded = false;
			this.pagebar = this.get("blkPageWheel");
			
			this.orgTypeId = null;
		};
		
		//user click the button to choose organization
		query.prototype.selectType = function(){
			//load the dialog
			var parent = ux.megatron.Main.getServer().getBodyBlock();
			var me = this;
            ux.megatron.Main.loadUIResourceAsync(parent, "ux.cdb.admin", "ux/cdb/admin/mechanism/cooOrg/ui/SelCooOrgTypeForSearch.xml", me, me.onBuilderLoadedForSelType);
		};
		//call back of the select type
		query.prototype.onBuilderLoadedForSelType=function(){
			var wizard = null;
			var parent = ux.megatron.Main.getServer().getBodyBlock();
			wizard = parent.getDescendantsOfType("ux.cdb.admin.mechanism.cooOrg.SelCooOrgTypeForSearch");
/* @JSC :: begin DEBUG */
			jsx3.log('wizard : ' + wizard[0]);
/* @JSC :: end */
			wizard[0].parent = this;
		};
		//when user selected type for search
		query.prototype.searchTypeSelected=function(orgType,orgTypeId){
			this.get("blkSearchType").getChild(1).setText(orgType,true);
			this.orgTypeId = orgTypeId;
		};
		
		query.prototype.onQuery = function(){
			var _data = this.queryDetail.getData();
			var _params = [
							_data.getFirstChild().getAttribute('internalOrgId'),
							_data.getFirstChild().getAttribute('branchCode'),
							_data.getFirstChild().getAttribute('cooperOrgName'),
							this.orgTypeId,
							this.currentPage, //current page
							this.pageSize //page size
						  ];

/* @JSC :: begin DEBUG */
			jsx3.log('query data : ' + _data + ' | params : ' + _params);
/* @JSC :: end */
			ux.megatron.util.Dwr.callDwr("RetailInfoQueryUCC","getCooperOrgListByCondition",_params,this,
				function(data){
					if (data != null && typeof data == 'object') {
/* @JSC :: begin DEBUG */
						jsx3.log('Query#doQuery data : ' + dwr.util.toDescriptiveString(data, 5));
/* @JSC :: end */
						this.setMatrixData(data);
					}
				});
		};

		query.prototype.setMatrixData = function(data) {
			this.loadPageControler(data.content.page.count); 
			data = data.content.data;
			if(data && data instanceof Array) {
				var tmpCDF = jsx3.xml.CDF.Document.newDocument();
				for(var i=0,item;item=data[i];i++){
					tmpCDF.insertRecord(item,null,false).setAttribute('jsxid',(i+1));
				}
				this.matrix.setSourceXML(tmpCDF);
				this.matrix.repaintData();
			} else {
				jsx3.log('Query#setMatrixData cdf data of matrix:' + data);
			}
		};
		
		query.prototype.loadPageControler = function(count) {
			this.totalRecordCount = count;
			this.totalPages = (count%this.pageSize>0) ? (parseInt(count/this.pageSize, 10) + 1) : (count/this.pageSize);
			if(!this.pageLoaded){
				var me = this;
				this.pagebar.initialize();
				this.pagebar.bindWithMatrix(me,me.onPageChange,me.matrix);
				this.pageLoaded = true;
			}
			this.pagebar.setValue(this.currentPage, this.totalPages, this.pageSize, this.totalRecordCount);
		};
		
		query.prototype.onPageChange = function(msg) {
			this.currentPage = msg.currentPageNumber;
			this.onQuery();
		};
		
		query.prototype.onReset = function(){
			this.queryDetail.setDataByObject({internalOrgId:'',branchCode:null,cooperOrgName:null,cooperOrgType:''});
			this.queryDetail.repaint();
		};
		
		query.prototype.matrixSelected = function(mtx,strRECORDID){
			var parent = ux.megatron.Main.getServer().getBodyBlock();
			var me = this;
			var rec = mtx.getRecord(strRECORDID);
			ux.megatron.Main.loadUIResourceAsync(parent,"ux.cdb.admin","ux/cdb/admin/queries/organ/ui/detaildialog.xml",me, me.dialogLoaded);
		};
		
		query.prototype.dialogLoaded = function(objEvent){
		};

    });
}